using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XnaUtilityTestbed
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //XnaUtility.Camera.KeyboardCamera camera;
        XnaUtility.Camera.UnrealEditCamera camera;
        XnaUtility.Control.InputBase input;
        XnaUtility.GameObjects.Base3DGameObject ship;

        XnaUtility.GameObjects.Base3DGameObjectManager objectManager;
        XnaUtility.FPS fps;

        Random rnd = new Random();

        float ar;
        Model myModel;

        float zDeath = 200.0f;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            input = new XnaUtility.Control.InputBase(this);
            input.LockMouse(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
            Components.Add(input);

            ar = (float)graphics.PreferredBackBufferWidth / (float)graphics.PreferredBackBufferHeight;
            camera = new XnaUtility.Camera.UnrealEditCamera(this, MathHelper.ToRadians(45), ar, 1.0f, 10000.0f);
            Components.Add(camera);
            Services.AddService(typeof(XnaUtility.Camera.CameraBase), camera);

            ship = new XnaUtility.GameObjects.Base3DGameObject(this, "ship");
            //Components.Add(ship);
            camera.Location = new Vector3(0, 0, 90);

            ship.velocity.Z = 25;

            objectManager = new XnaUtility.GameObjects.Base3DGameObjectManager(this);
            Components.Add(objectManager);
            objectManager.AddGameObject(ship);

            fps = new XnaUtility.FPS(this);

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            myModel = Content.Load<Model>("ship");
            ship.ForceLoadContent();

            fps.FontColor = Color.White;
            fps.Location = new Vector2(50, 50);
            fps.Font = Content.Load<SpriteFont>("fpsFont");
            fps.SprBatch = spriteBatch;
            Components.Add(fps);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();

            if (input.WasKeyReleased(Keys.R)) ResetGame();

            if (input.WasKeyPressed(Keys.Space)) input.ToggleMouseLock();

            if (input.WasKeyReleased(Keys.T))
            {
                XnaUtilitySolution.ShipObject obj = new XnaUtilitySolution.ShipObject(this, "xwing");
                obj.location = new Vector3(0, 0, -100);
                obj.velocity.Z = 0;
                obj.ZDeath = zDeath;
                objectManager.AddGameObject(obj, true);
            }

            if (input.WasKeyReleased(Keys.G))
            {
                for (int i = 0; i < 50; i++)
                {
                    XnaUtilitySolution.ShipObject obj = new XnaUtilitySolution.ShipObject(this, "xwing");
                    obj.location = new Vector3(rnd.Next(251) - 125, rnd.Next(50) - 25, rnd.Next(75) - 155);
                    obj.velocity.Z = 50 + rnd.Next(15);
                    obj.ZDeath = zDeath;
                    objectManager.AddGameObject(obj, true);
                }
            }

            if (input.WasKeyReleased(Keys.Add)) zDeath += 15.0f;
            if (input.WasKeyReleased(Keys.Subtract)) zDeath -= 15.0f;


            if(input.WasKeyPressed(Keys.Escape)) this.Exit();

            base.Update(gameTime);
        }

        public void ResetGame()
        {
            //stub
        }



        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            
            base.Draw(gameTime);
        }
    }
}
